home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.arch.arithmetic,comp.lang.c,comp.lang.c++
- Path: cwi.nl!dik
- From: dik@cwi.nl (Dik T. Winter)
- Subject: Re: Access carry flag from C
- Message-ID: <Dnz4AA.Jv2@cwi.nl>
- Sender: news@cwi.nl (The Daily Dross)
- Nntp-Posting-Host: chrysant.cwi.nl
- Organization: CWI, Amsterdam
- References: <4hhbt8$1d3o@b.stat.purdue.edu> <Dnssxu.JGy@cwi.nl> <313D385C.4651@xnet.otm.fi>
- Date: Sat, 9 Mar 1996 00:06:10 GMT
-
- In article <313D385C.4651@xnet.otm.fi> Jouko Holopainen <jouko.holopainen@xnet.otm.fi> writes:
- > > > > if (x + yl < yl) ++yh; /* test for overflow on next line */
- > > > > yl += x;
- ...
- > Just for fun, let's compare carry and non-carry assembly
- > (kinda x86):
- > Without carry:
- ...
- > With carry:
- ...
- > See any difference?
-
- Yes, I see a difference. So what?
- >
- > Optimizing an addition away might in fact slow down. It just
- > depends whether tmp and/or x,yl,yh are register variables.
-
- If it slows down it is not an optimization and so an optimizer should not
- do it.
- ...
- > > But the code above also works on machines without carry bit.
- >
- > Yes. In fact, the latter cannot be produced with C
- > (or any other high level language, for that matter).
- > Which was the original point.
-
- No? But if a compiler sees the ideom:
- unsigned long x, yl, yh;
- ...
- if(x + yl < yl) yh++; yl += x;
- he is entitled to create code that uses the carry bit from the addition
- in the subsequent increment. Although I do not know any real life compiler
- that does this I know there exists a super optimizer that does such
- transformations. You should not expect that code as you present it to
- a compiler will be compiled in a literal fashion. I know of some
- compilers where it would hard to deduct the original code from the
- assembly generated (*).
-
- On the other hand, suppose there is a function carry that returns the
- carry bit of the operation embedded in the parameter list (yes, it is
- a bit difficult to formalize) so that you can write:
- yh += carry(yl += x);
- or something like that. It is clear that on a machine that has no
- carry bits this should be translated to the ideom above, as it can
- not be translated directly.
- --
- (*) Suppose you see the following assembler equivalent in pseudo C:
- vector register float v[128]; int i; float f;
- v[0:128] = 1.0; /* Meaning: set 128 elements of v from element 0 to 1 */
- for(i = 0; i < 8192; i+= 128) v[0:128] *= 2.0;
- v[0:64] *= v[64:64];
- v[0:32] *= v[32:32];
- v[0:16] *= v[16:16];
- v[0:16] *= v[1:16];
- f = v[0] * v[2] * v[4] * v[6] * v[8] * v[10] * v[12] * v[14];
- would you expect that the original (real C) code was:
- f = 1.0;
- for(i = 0; i < 8192; i++) f *= 2.0;
- ? (Why so difficult? It is faster on that machine.)
- --
- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924098
- home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
-